package org.acompanhamento.pages.sprint;

import java.util.ArrayList;
import java.util.List;

import jmine.tec.di.annotation.Injected;
import jmine.tec.web.wicket.pages.form.FormPage;
import jmine.tec.web.wicket.pages.form.FormType;

import org.acompanhamento.HelpMessages;
import org.acompanhamento.dao.WorkflowDAO;
import org.acompanhamento.domain.Sprint;
import org.acompanhamento.domain.Team;
import org.acompanhamento.domain.Workflow;
import org.apache.wicket.Component;
import org.apache.wicket.Page;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.request.mapper.parameter.PageParameters;

import bancosys.tec.exception.MessageCreator;
import bancosys.tec.persist.dao.BaseDAO;
import bancosys.tec.security.annotation.Secure;
import bancosys.tec.security.authorization.UrlPermission;

/**
 * 
 * @author icaro.kume
 *
 */
@Secure(id = "GENERIC_TEAM_MEMBER_URL", permissionType = UrlPermission.class)
public class EditSprintPage extends FormPage<Sprint> {

	@Injected
	private BaseDAO<Team> teamDAO;

	@Injected
	private WorkflowDAO workflowDAO;

	public EditSprintPage(PageParameters pageParameters) {
		super(pageParameters);
	}

	public EditSprintPage(Page sourcePage, PageParameters sourcePageParameters, Sprint entity, FormType formType) {
		super(sourcePage, sourcePageParameters, entity, formType);
	}

	public EditSprintPage(PageParameters pageParameters, Page sourcePage) {
		super(pageParameters, sourcePage);
	}

	@Override
	protected List<Component> createFormComponents() {
		List<Component> components = new ArrayList<Component>();
		components.add(this.createDateField("start", true));
		components.add(this.createDateField("end", true));
		DropDownChoice<Team> dropDownTeam = this.createDropDown("team", this.teamDAO.findAll());
		final DropDownChoice<Workflow> dropDownWorkflow = this.createDropDown("workflow", this.workflowDAO.findAll());
		components.add(dropDownWorkflow);
		dropDownWorkflow.setOutputMarkupId(true);
		dropDownTeam.add(new AjaxFormComponentUpdatingBehavior("onblur") {
			@Override
			protected void onUpdate(AjaxRequestTarget target) {
				dropDownWorkflow.setChoices(EditSprintPage.this.workflowDAO.findByTeam(EditSprintPage.this.getEntity().getTeam()));
				target.add(dropDownWorkflow);
			}
		});
		components.add(dropDownTeam);
		components.add(new TextArea<Sprint>("summary").setVisible(!this.getFormType().isNew()));
		return components;
	}

	@Override
	protected MessageCreator getHelpTextCreator() {
		return HelpMessages.TEAMS;
	}
}
